import axios from 'axios'
// var root = "http://dp.yywuyu.xyz/api/index/"
import {getStore} from "@/common/util";
import store from '../store'
import {ips} from "../../static/config"
//  https://xf2.huawnh.cn http://10.94.94.50 http://xf.cn
let baseurl = ips.includes(window.location.hostname) ?  window.location.origin : 'http://10.94.94.50'
console.log(window.location);

// let baseurl = ''
store.state.baseUrl = baseurl
const service = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: store.state.baseUrl +  "/index.php/api/index",  //不加index可以访问  打包后台要加index
    // baseURL:'/prod-api',
    // 超时
    timeout: 15000
})

//请求拦截器
service.interceptors.request.use(function (config) {
    store.state.showLoading=true;
    config.headers.token = getStore('token')?getStore('token'):''
    config.headers.userId = getStore('userId')?getStore('userId'):''
    return config
}, function (error) {
    // 请求出错做什么
    return Promise.reject(error)
})
// 响应拦截器
service.interceptors.response.use(function (response) {
    store.state.showLoading=false;
    let responseStatus = response.data.code
    if(responseStatus === 0&&(response.data.msg == '请重新登陆'||response.data.msg == '未注册'||response.data.msg == '登陆态已过期')){
        window.location.href = "/#/login";
    }else{
        return response.data
    }
}, function (error) {
    return Promise.reject(error)
})

// axios请求封装
export default function http(url = '', data = {}, type = 'POST') {
    return service({
        url: url,
        method: type,
        data: data
    })
}
